import LinvoDB from './api'
var modelName = "payment";
var schema = { }; // Non-strict always, can be left empty
var options = { };

//创建数据表
// const paymentDB = new LinvoDB(modelName, schema,options)

export default class PaymentDao {
    //数据库操作工具方法
    static insertData(data) {
        return new Promise((resolve, reject) => {
            let paymentDB = new LinvoDB(modelName, schema,options)
            paymentDB.insert(data,function (err,newInvoice) {
                // console.log(666,newInvoice)
                resolve(newInvoice)
            })
        })
    }

    static findListNoPage(data) {
        return new Promise((resolve, reject) => {
            let paymentDB = new LinvoDB(modelName, schema,options)
            paymentDB.find(data).sort({"createAt":-1})
                .exec(function (err, results) {
                    resolve(results);
                });
        })
    }

    static findList(data,startPages,pageSize) {
        return new Promise((resolve, reject) => {
            let paymentDB = new LinvoDB(modelName, schema,options)
            paymentDB.find(data).sort({"createAt":-1})
                .skip(startPages).limit(pageSize)
                .exec(function (err, results) {
                    resolve(results);
                });
        })
    }

    static removeData(data) {
        return new Promise((resolve, reject) => {
            let paymentDB = new LinvoDB(modelName, schema,options)
            paymentDB.remove(data,{ multi: true },function (err,newInvoice) {
                // console.log(666,newInvoice)
                resolve(err)
            })
        })
    }

    static updateData(query,data) {
        return new Promise((resolve, reject) => {
            let paymentDB = new LinvoDB(modelName, schema,options)
            paymentDB.update(query,{ $set: data},{},function (err,numReplaced) {
                // console.log(666,newInvoice)
                resolve(err)
            })
        })
    }

    static count(searchParam) {
        return new Promise((resolve, reject) => {
            let paymentDB = new LinvoDB(modelName, schema,options)
            paymentDB.count(searchParam,function (err, count) {
                console.log('[count err]',err)
                resolve(count);
            })
        })
    }
}